//
//  GatherInfo.m
//  YYTextTest
//
//  Created by macintosh on 16/9/12.
//  Copyright © 2016年 macintosh. All rights reserved.
//

#import "GatherInfo.h"
#import "InfoArchiver.h"

@implementation GatherInfo

+ (NSString *)gatherInfoArchiverPath
{
    return [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"gatherUserInfo.archiver"];
}

+ (GatherInfo *)gatherInfo
{
    GatherInfo *info = [InfoUnarchiver unarchiveObjectWithFile:[GatherInfo gatherInfoArchiverPath]];
    
    if (!info) {
        info = [self defaultGatherInfo];
    }
    
    return info;
}

+ (GatherInfo *)defaultGatherInfo
{
    GatherInfo *info = [GatherInfo new];
    info.showLogo = YES;
    info.isFrontCamera = YES;
    info.showBarrage = YES;
    info.openTorch = YES;
    info.openBeauty = YES;
    info.isLandscape = NO;
    info.isMute = NO;
    
    info.resolution = 720;
    info.audioRate = 128;
    info.videoRate = 1000;
    info.fps = 20;
    return info;
}

+ (void)archiveGatherInfo:(GatherInfo *)gatherInfo
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [InfoArchiver archiveRootObject:gatherInfo toFile:[self gatherInfoArchiverPath]];
    });
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeBool:_showLogo forKey:@"showLogo"];
    [aCoder encodeBool:_isFrontCamera forKey:@"isFrontCamera"];
    [aCoder encodeBool:_showBarrage forKey:@"showBarrage"];
    [aCoder encodeBool:_openTorch forKey:@"openTorch"];
    [aCoder encodeBool:_openBeauty forKey:@"openBeauty"];
    [aCoder encodeBool:_isLandscape forKey:@"isLandscape"];
    [aCoder encodeBool:_isMute forKey:@"isMute"];
    
    [aCoder encodeInteger:_resolution forKey:@"resolution"];
    [aCoder encodeInteger:_audioRate forKey:@"audioRate"];
    [aCoder encodeInteger:_videoRate forKey:@"videoRate"];
    [aCoder encodeFloat:_fps forKey:@"fps"];
}

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    GatherInfo *info = [GatherInfo new];
    info.showLogo = [aDecoder decodeBoolForKey:@"showLogo"];
    info.isFrontCamera = [aDecoder decodeBoolForKey:@"isFrontCamera"];
    info.showBarrage = [aDecoder decodeBoolForKey:@"showBarrage"];
    info.openTorch = [aDecoder decodeBoolForKey:@"openTorch"];
    info.openBeauty = [aDecoder decodeBoolForKey:@"openBeauty"];
    info.isLandscape = [aDecoder decodeBoolForKey:@"isLandscape"];
    info.isMute = [aDecoder decodeBoolForKey:@"isMute"];
    
    info.resolution = [aDecoder decodeIntForKey:@"resolution"];
    info.audioRate = [aDecoder decodeIntForKey:@"audioRate"];
    info.videoRate = [aDecoder decodeIntForKey:@"videoRate"];
    info.fps = [aDecoder decodeFloatForKey:@"fps"];
    return info;
}

- (id)copyWithZone:(NSZone *)zone {
    GatherInfo *info = [[[self class] allocWithZone:zone] init];
    info.showLogo = self.showLogo;
    info.isFrontCamera = self.isFrontCamera;
    info.showBarrage = self.showBarrage;
    info.openTorch = self.openTorch;
    info.openBeauty = self.openBeauty;
    info.isLandscape = self.isLandscape;
    info.isMute = self.isMute;
    
    info.resolution = self.resolution;
    info.audioRate = self.audioRate;
    info.videoRate = self.videoRate;
    info.fps = self.fps;
    return info;
}

- (BOOL)isEqual:(id)object
{
    BOOL isEqual = NO;
    
    if (self == object) {
        return YES;
    }
    
    if ([object isKindOfClass:[GatherInfo class]]) {
        GatherInfo *info = object;
        if (self.showLogo == info.showLogo &&
            self.isFrontCamera == info.isFrontCamera &&
            self.showBarrage == info.showBarrage &&
            self.openTorch == info.openTorch &&
            self.openBeauty == info.openBeauty &&
            self.isLandscape == info.isLandscape &&
            self.isMute == info.isMute &&
            self.resolution == info.resolution &&
            self.audioRate == info.audioRate &&
            self.videoRate == info.videoRate &&
            self.fps == info.fps) {
            return YES;
        }
    }
    
    return isEqual;
}

@end
